import axios from 'axios'

const API_BASE_URL = '/api'

const apiClient = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  },
})

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API Error:', error)
    throw error
  }
)

// 获取所有问卷
export const getSurveys = async () => {
  const response = await apiClient.get('/surveys/')
  return response.data
}

// 获取单个问卷
export const getSurvey = async (id) => {
  const response = await apiClient.get(`/surveys/${id}/`)
  return response.data
}

// 创建问卷
export const createSurvey = async (data) => {
  const response = await apiClient.post('/surveys/', data)
  return response.data
}

// 更新问卷
export const updateSurvey = async (id, data) => {
  const response = await apiClient.put(`/surveys/${id}/`, data)
  return response.data
}

// 删除问卷
export const deleteSurvey = async (id) => {
  const response = await apiClient.delete(`/surveys/${id}/`)
  return response
}
